package com.miaomiao.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.miaomiao.common.Result;
import com.miaomiao.domain.system.RegisterBody;
import com.miaomiao.domain.system.User;
import com.miaomiao.service.system.RegisterService;
import com.miaomiao.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/register")
@Api(tags = "用户注册")
public class RegisterController {

    @Autowired
    UserService userService;

    @Autowired
    RegisterService registerService;


    @GetMapping("/check")
    @ApiOperation("检查用户名是否可用")
    public Result checkUserName(@RequestParam String userName){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,userName);
        User user = userService.getOne(queryWrapper);
        if(!Objects.isNull(user)){
            return Result.error("用户名已存在");
        }else{
            return Result.success("用户名合法");
        }
    }

    @GetMapping("/sendCode")
    @ApiOperation("发送邮箱验证码")
    public Result sendCode(@RequestParam String email){
        return registerService.senCode(email);
    }

    @PostMapping
    @ApiOperation("用户注册")
    public Result registerUser(@RequestBody RegisterBody registerBody){
        return registerService.registerUser(registerBody);
    }
}
